package com.tiger.cloud.controller.crm;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.tiger.cloud.common.mapper.JsonMapper;
import com.tiger.cloud.common.mybatis.BaseController;
import com.tiger.cloud.enums.DeleteEnum;
import com.tiger.cloud.filter.CurrentUserId;
import com.tiger.cloud.form.SysDictSmall;
import com.tiger.cloud.model.crm.Customer;
import com.tiger.cloud.model.crm.LoanInfo;
import com.tiger.cloud.service.crm.ICustomerService;
import com.tiger.cloud.service.crm.ILoanInfoService;
import com.tiger.cloud.service.system.CommonDict;
import com.tiger.cloud.util.Page;
import com.tiger.cloud.util.Response;
import com.tiger.cloud.util.ResponseUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 人脉圈-->客户圈
 */
@Controller
@RequestMapping(value = "customer")
@Api(value = "人脉圈-->客户圈", tags = "客户圈")
public class CustomerController extends BaseController {

	@Autowired
	private ICustomerService customerService;
	@Autowired
	private ILoanInfoService loanInfoService;

	/**
	 * 获取列表默认值
	 */
	@RequestMapping(value = "getDefaultFileds", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	@ApiOperation(value = "获取列表默认值", notes = "获取列表默认值")
	public Response getDefaultFileds() {
		Map<String, List<SysDictSmall>> values = CommonDict.getFileds("customer_info");
		return ResponseUtil.ok(values);
	}

	/**
	 * 增量获取获取客户圈
	 */
	@RequestMapping(value = "incrementRecommands", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	@ApiOperation(value = "增量获取获取客户圈", notes = "增量获取获取客户圈")
	public Response incrementRecommands(@ApiParam(name = "id", value = "用户id", hidden = true) @CurrentUserId Long userId,@RequestBody Customer customer) {
		customer.setIsDelete(DeleteEnum.NOMAIL);
		customer.setUserId(userId);
		List<Customer> customerList = customerService.find(customer);
		
		if(customerList!=null){
			List<Long> customerIds=new ArrayList<Long>();
			customerList.forEach(customerObj -> {
				customerIds.add(customerObj.getId());
			});
			if(customerIds==null||customerIds.size()==0){
				return ResponseUtil.ok(customerList);
			}
			List<LoanInfo> loanInfos=loanInfoService.findByCustomerIds(customerIds);
			if(loanInfos!=null){
				for (Customer customerObj :customerList) {
					for (LoanInfo loanInfo : loanInfos) {
						if(customerObj.getId().longValue()==loanInfo.getCustomerId().longValue()){
							customerObj.getLoanInfos().add(loanInfo);
						}
					}
				}
			}
		}
		
		return ResponseUtil.ok(customerList);
	}

	/**
	 * 增量分页获取客户圈
	 */
	@RequestMapping(value = "find", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	@ApiOperation(value = "增量分页获取获取客户圈", notes = "增量分页获取获取客户圈")
	public Response find(@ApiParam(name = "id", value = "用户id", hidden = true) @CurrentUserId Long userId,@RequestBody Customer customer) {
//		customer.setIsDelete(DeleteEnum.NOMAIL);
		customer.setUserId(userId);
		Page<Customer> datas = customerService.page(customer);
		Map<String, List<SysDictSmall>> values = CommonDict.getFileds("customer_info");
		datas.setDefaultValues(values);
		
		if(datas.getData()!=null){
			List<Long> customerIds=new ArrayList<Long>();
			datas.getData().forEach(customerObj -> {
				customerIds.add(customerObj.getId());
			});
			if(customerIds==null||customerIds.size()==0){
				return ResponseUtil.ok(datas);
			}
			List<LoanInfo> loanInfos=loanInfoService.findByCustomerIds(customerIds);
			if(loanInfos!=null){
				for (Customer customerObj : datas.getData()) {
					for (LoanInfo loanInfo : loanInfos) {
						if(customerObj.getId().longValue()==loanInfo.getCustomerId().longValue()){
							customerObj.getLoanInfos().add(loanInfo);
						}
					}
				}
			}
		}
		
		
		return ResponseUtil.ok(datas);
	}
	
	/**
	 * 获取单条客户圈
	 */
	@RequestMapping(value = "get", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	@ApiOperation(value = "获取单条获取客户圈", notes = "获取单条获取客户圈")
	public Response get(@ApiParam(name = "id", value = "用户id", hidden = true) @CurrentUserId Long userId,@RequestBody Customer customer) {
		customer.setIsDelete(DeleteEnum.NOMAIL);
		customer.setUserId(userId);
		Customer customer2 = customerService.findOne(customer);
		if(customer2!=null){
			List<Long> customerIds=new ArrayList<Long>();
			customerIds.add(customer2.getId());
			if(customerIds==null||customerIds.size()==0){
				return ResponseUtil.ok(customer2);
			}
			List<LoanInfo> loanInfos=loanInfoService.findByCustomerIds(customerIds);
			if(loanInfos!=null){
				for (LoanInfo loanInfo : loanInfos) {
					if(customer2.getId().longValue()==loanInfo.getCustomerId().longValue()){
						customer2.getLoanInfos().add(loanInfo);
					}
				}
			}
		}
		return ResponseUtil.ok(customer2);
	}

	/**
	 * 客户更新
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	@ApiOperation(value = "更新客户", notes = "更新客户")
	public Response update(@ApiParam(name = "id", value = "用户id", hidden = true) @CurrentUserId Long userId,@RequestBody Customer customer) {
		customer.setIsDelete(DeleteEnum.NOMAIL);
		customer.setUserId(userId);
		if (customerService.updateCustomer(customer)) {
			Customer customer2 = customerService.get(customer.getId());
			if(customer2!=null){
				List<Long> customerIds=new ArrayList<Long>();
				customerIds.add(customer2.getId());
				if(customerIds==null||customerIds.size()==0){
					return ResponseUtil.ok(customer2);
				}
				List<LoanInfo> loanInfos=loanInfoService.findByCustomerIds(customerIds);
				if(loanInfos!=null){
					for (LoanInfo loanInfo : loanInfos) {
						if(customer2.getId().longValue()==loanInfo.getCustomerId().longValue()){
							customer2.getLoanInfos().add(loanInfo);
						}
					}
				}
				return ResponseUtil.ok(customer2);
			}
		}
		return ResponseUtil.error();
	}

	/**
	 * 新增客户
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	@ApiOperation(value = "新增客户", notes = "新增客户")
	public Response add(@ApiParam(name = "id", value = "用户id", hidden = true) @CurrentUserId Long userId,
			@RequestBody Customer customer) {
		customer.setIsDelete(DeleteEnum.NOMAIL);
		customer.setUserId(userId);
		System.out.println(JsonMapper.nonDefaultMapper().toJson(customer));
		if (customerService.insertCustomer(customer)) {
			Customer customer2 = customerService.get(customer.getId());
			if(customer2!=null){
				List<Long> customerIds=new ArrayList<Long>();
				customerIds.add(customer2.getId());
				if(customerIds==null||customerIds.size()==0){
					return ResponseUtil.ok(customer2);
				}
				List<LoanInfo> loanInfos=loanInfoService.findByCustomerIds(customerIds);
				if(loanInfos!=null){
					for (LoanInfo loanInfo : loanInfos) {
						if(customer2.getId().longValue()==loanInfo.getCustomerId().longValue()){
							customer2.getLoanInfos().add(loanInfo);
						}
					}
				}
				return ResponseUtil.ok(customer2);
			}
		}
		return ResponseUtil.error();
	}

	/**
	 * 删除通客户
	 */
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	@ApiOperation(value = "删除客户", notes = "删除客户")
	public Response delete(@ApiParam(name = "id", value = "用户id", hidden = true) @CurrentUserId Long userId,
			@RequestBody Customer customer) {
		if (customer.getId() != null && customer.getId() > 0) {
			Customer deleteEntity = customerService.get(customer.getId());
			if (deleteEntity == null || deleteEntity.getUserId() != userId) {
				return ResponseUtil.error("参数错误,请重试!");
			}
			deleteEntity.setIsDelete(DeleteEnum.DELETE);
			if (customerService.deleteCustomer(deleteEntity)) {
				return ResponseUtil.ok(deleteEntity);
			}
			return ResponseUtil.error();
		}
		return ResponseUtil.error("参数错误,请重试!");
	}

}